package com.weiyuwqj.admin.controller.admin.financial;

import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.model.BusItemCollect;
import com.weiyuwqj.api.model.BusItemRepay;
import com.weiyuwqj.api.model.convert.InvestTypeConvert;
import com.weiyuwqj.api.model.convert.ItemAddtimeConvert;
import com.weiyuwqj.api.model.convert.ItemRepayMethodConvert;
import com.weiyuwqj.api.model.convert.ItemStatusConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.AccountCheckingVo;
import com.weiyuwqj.api.model.vo.InvestCollectVo;
import com.weiyuwqj.api.service.BasItemService;
import com.weiyuwqj.api.service.BusInvestCollectService;
import com.weiyuwqj.api.service.BusInvestRepayService;
import com.weiyuwqj.api.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huzj on 2017/4/25.
 */
@Controller
@RequestMapping("/admin/accountChecking")
public class AccountCheckingController {
    @Autowired
    BasItemService itemService;

    @Autowired
    BusInvestRepayService repayService;

    @Autowired
    BusInvestCollectService collectService;


    //对账管理列表
    @ResponseBody
    @RequestMapping("/list")
    public Object list(Paginator p,
                                   String beginDateTime,
                                   String endDateTime,
                                   String itemStatus,
                                   String companyName,
                                   Integer itemRepayMethod,
                                   String itemName) {
        Map map = new HashMap();
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        map.put("itemStatus",itemStatus);
        map.put("companyName",companyName);
        map.put("itemRepayMethod",itemRepayMethod);
        map.put("itemName",itemName);
        return itemService.checkAccountList(p,map);
    }

    //对账管理列表
    @ResponseBody
    @RequestMapping("/excelList")
    public Object excelList(
            String beginDateTime,
            String endDateTime,
            Integer itemStatus,
            Integer companyId,
            Integer itemRepayMethod,
            String itemName,
            HttpServletRequest request) {
        String[] cArray = {"项目名", "资产方", "项目金额", "资产利率", "项目利率", "期数", "已收小计", "已收本金", "已收利息",
                "余收小计", "余收本金", "余收利息", "已付小计", "已付本金", "已付利息", "余付小计", "余付本金", "余付利息", "项目状态"};
        String[] eArray = {"ItemName", "CompanyName", "ItemAccount", "ProductRate", "ItemRate", "Periods", "RecivedAmount",
                "RecivedPrincipal", "RecivedInterest", "RestReciveAmount", "RestRecivePrincipal", "RestReciveInterest",
                "PaidAmount", "PaidPrincipal", "PaidInterest", "RestPayAmount", "RestPayPrincipal", "RestPayInterest", "ItemStatus"};
        ModelConvert[] converts = {null, null, null, null, null,null, null, null, null, null,null, null, null, null, null,
                null, null, null, new ItemStatusConvert()};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        map.put("itemStatus",itemStatus);
        map.put("companyId",companyId);
        map.put("itemRepayMethod",itemRepayMethod);
        map.put("itemName",itemName);

        List<AccountCheckingVo> contentList = itemService.checkAccountList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("对账管理列表", cList, eList, convertList, contentList, serverDir);

        HashMap ret =new HashMap();
        ret.put("result","success");
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    //对账管理列表
    @ResponseBody
    @RequestMapping("/stat")
    public Object stat(String beginDateTime,
                       String endDateTime,
                       Integer itemStatus,
                       Integer companyId,
                       Integer itemRepayMethod,
                       String itemName) {
        Map map = new HashMap();
        map.put("beginDateTime",beginDateTime);
        map.put("endDateTime",endDateTime);
        map.put("itemStatus",itemStatus);
        map.put("companyId",companyId);
        map.put("itemRepayMethod",itemRepayMethod);
        map.put("itemName",itemName);
        Map map1 = repayService.checkAccountStat(map);
        Map map2 = collectService.checkAccountStat(map);
        Map map3 = itemService.checkAccountStat(map);
        map1.putAll(map2);
        map1.putAll(map3);
        return map1;
    }

    //还款明细
    @ResponseBody
    @RequestMapping("/getRepayListByItemId")
    public Object getRepayListByItemId(Integer itemId) {
        List<BusItemRepay> list = repayService.getRepayListByItemId(itemId);
        return list;
    }

    //还款明细统计
    @ResponseBody
    @RequestMapping("/statRepayByItemId")
    public Object statRepayByItemId(Integer itemId) {
        Map map = repayService.statRepayByItemId(itemId);
        return map;
    }

    //投资方付款明细
    @ResponseBody
    @RequestMapping("/getCollectByItemId")
    public Object getCollectByItemId(Integer itemId) {
        List<InvestCollectVo> list = collectService.getCollectByItemId(itemId);
        return list;
    }

    //投资方付款明细统计
    @ResponseBody
    @RequestMapping("/statCollectByItemId")
    public Object statCollectByItemId(Integer itemId) {
        Map map = collectService.statCollectByItemId(itemId);
        return map;
    }

    //展开每期金额明细
    @ResponseBody
    @RequestMapping("/getCollectByItemIdAndUserId")
    public Object getCollectByItemIdAndUserId(Integer itemId,Integer userId) {
        List<BusItemCollect> list = collectService.getCollectByItemIdAndUserId(itemId,userId);
        return list;
    }


}
